package com.mab.mall.search.thread;

import java.util.concurrent.*;

/**
 * 异步处理
 */
public class CompletableFutureDemo {

    // 自定义线程池
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(5
            ,10
            ,10
            , TimeUnit.SECONDS
            , new LinkedBlockingQueue<>(100)
            , Executors.defaultThreadFactory()
            , new ThreadPoolExecutor.AbortPolicy()
            );

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 异步对象创建
        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(() -> {
            System.out.println("线程执行开始---->");
            int i = 10 / 2;
            System.out.println("线程执行结束--------");
        }, executor);

        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println("线程执行开始---->");
            int i = 10 / 2;
            System.out.println("线程执行结束--------");
            return i;
        }, executor);
        System.out.println("supply线程执行结果--->"+supplyAsync.get());

    }

}
